/*
 * Card and Account Instrument 
 * Adding new and updating code 
 */
package com.moment.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;

import com.moment.model.InstrumentBankData;
import com.moment.model.InstrumentCardData;
import com.moment.model.WalletData;
import com.moment.util.Converter;
import com.moment.webservice.Services;
import com.moment.webservice.ServicesProxy;



@WebServlet("/CardController")
public class CardController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public CardController() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doCall(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doCall(request, response);
	}

	protected void doCall(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String target = null;
		String callto = request.getParameter("instrumenttype");// call for which
																// instrument
		System.out.println("instrumentType=" + callto);
		if (callto.equals("Card")) { // instrument type card
			String editOrSave = request.getParameter("updateorsave");// update
																		// or
																		// save
																		// card
																		// instrument
			if (editOrSave.equalsIgnoreCase("update")) {// update card
														// instrument
				String mobilenumber = (String) request.getSession()
						.getAttribute("username");
				String nickname = request.getParameter("nickname");
				String cardno = request.getParameter("cardno");
				String cardexpirym = request.getParameter("expirym");
				String cardexpiryy = request.getParameter("expiryy");
				String cardtype = request.getParameter("cardtype");
				String instruDlimit = request.getParameter("instruDlimit");
				String instruMlimit = request.getParameter("instruMlimit");
				String currency_Id = request.getParameter("currency");
				System.out.println(mobilenumber + " " + nickname + " " + cardno
						+ " " + cardexpirym+" "+cardexpiryy);
				InstrumentCardData instrumentObj = (InstrumentCardData) request
						.getSession().getAttribute("instrumentObj");
				// --New CODE-------------
				Map<String, String> map = new HashMap<String, String>();
				map.put("method", "25");
				map.put("instrumentname", nickname);
				map.put("cardnumber", cardno);
				map.put("cardexpiry", cardexpirym+"-"+cardexpirym);
				map.put("cardtype", cardtype);
				map.put("dlimit", instruDlimit);
				map.put("mlimit", instruMlimit);
				map.put("instid", instrumentObj.getInstruId());
				map.put("currencyId", currency_Id);
				
				String json = new Converter().mapToJson(map);
				System.out.println("json String :" + json);
				Services serviceproxy = new ServicesProxy();
				String output = serviceproxy.service(json);
				// ---------------------------

				// WebsProxy proxy=new WebsProxy();
				// String output=proxy.updateCard(nickname, cardno, cardexpiry,
				// cardtype, instruDlimit, instruMlimit,
				// instrumentObj.getInstruId());

				System.out.println("data from web Service:=" + output);
				HttpSession session = request.getSession();
				if (output.equals("Success")) {
					// --New CODE-------------
					Map<String, String> map1 = new HashMap<String, String>();
					map1.put("method", "16");
					map1.put("mnum", mobilenumber);
					json = new Converter().mapToJson(map1);
					System.out.println("json String :" + json);
					Services serviceproxy1 = new ServicesProxy();

					String out = serviceproxy1.service(json);
					// ---------------------------

					// String out=proxy.fetchInstruments(mobilenumber);
					System.out.println("out=" + out);

					WalletData walletObj = (WalletData) session
							.getAttribute("walletObj");
					walletObj.setInstru(out);
					session.setAttribute("instrumentObj", null);
					session.setAttribute("success", "Instrument Updated");
					// target="jsp/viewprofile.jsp";
					response.sendRedirect("jsp/viewprofile.jsp");
				} else {
					// target="jsp/addcard.jsp";
					session.setAttribute("error", "Instrument Not Added");
					response.sendRedirect("jsp/addinstrument.jsp");
				}
			} else {// Saving card instrument
				String mobilenumber = (String) request.getSession()
						.getAttribute("username");
				String nickname = request.getParameter("nickname");
				String cardno = request.getParameter("cardno");
				String cardexpirym = request.getParameter("expirym");
				String cardexpiryy = request.getParameter("expiryy");
				String cardtype = request.getParameter("cardtype");
				String instruDlimit = request.getParameter("instruDlimit");
				String instruMlimit = request.getParameter("instruMlimit");
				String currency_Id = request.getParameter("currency");
				System.out.println(mobilenumber + " " + nickname + " " + cardno
						+ " " + cardexpirym+" "+cardexpiryy);
				// --New CODE-------------
				Map<String, String> map = new HashMap<String, String>();
				map.put("method", "6");
				map.put("mnum", mobilenumber);
				map.put("nickname", nickname);
				map.put("cardnumber", cardno);
				map.put("cardexpiry", cardexpirym+"-"+cardexpiryy);
				map.put("cardtype", cardtype);
				map.put("dlimit", instruDlimit);
				map.put("mlimit", instruMlimit);
				map.put("currencyId", currency_Id);
				String json = new Converter().mapToJson(map);
				System.out.println("json String :" + json);
				Services serviceproxy = new ServicesProxy();

				String output = serviceproxy.service(json);
				// ---------------------------
				// WebsProxy proxy=new WebsProxy();
				// String output=proxy.addInstrumentCard(mobilenumber, nickname,
				// cardno, cardexpiry, cardtype, instruDlimit, instruMlimit);
				System.out.println("data from web Service:=" + output);
				HttpSession session = request.getSession();
				if (output.equals("Success")) {
					// --New CODE-------------
					Map<String, String> map1 = new HashMap<String, String>();
					map1.put("method", "16");
					map1.put("mnum", mobilenumber);
					json = new Converter().mapToJson(map1);
					System.out.println("json String :" + json);
					Services serviceproxy1 = new ServicesProxy();

					String out = serviceproxy1.service(json);
					// ---------------------------
					// String out=proxy.fetchInstruments(mobilenumber);
					System.out.println("out=" + out);
					WalletData walletObj = (WalletData) session
							.getAttribute("walletObj");
					walletObj.setInstru(out);
					// target="jsp/viewprofile.jsp";
					session.setAttribute("success", "Instrument Added");
					response.sendRedirect("jsp/viewprofile.jsp");
				} else {
					// target="jsp/addcard.jsp";
					session.setAttribute("error", "Instrument Not Added");
					response.sendRedirect("jsp/addinstrument.jsp");
				}
			}
		} else {// Account instrument

			String updateOrSave = request.getParameter("updateorsave");
			System.out.println("update or save" + updateOrSave);
			if (updateOrSave.equalsIgnoreCase("update")) {// updating Account
															// INstrument
				String mobilenumber = (String) request.getSession()
						.getAttribute("username");
				String nickname = request.getParameter("nickname");
				String bankAccno = request.getParameter("bankAccno");
				String bankName = request.getParameter("bankName");
				String accType = request.getParameter("acctype");
				String instruDlimit = request.getParameter("instruDlimit");
				String instruMlimit = request.getParameter("instruMlimit");
				String currency_Id = request.getParameter("currency");
				System.out.println(mobilenumber + " " + nickname + " "
						+ bankAccno + " " + accType + " " + instruDlimit + " "
						+ instruMlimit);
				InstrumentBankData instrumentObj = (InstrumentBankData) request
						.getSession().getAttribute("instrumentObj");
				System.out.println("instrumentObj.getIntruId() "
						+ instrumentObj.getIntruId());

				// --New CODE-------------
				Map<String, String> map = new HashMap<String, String>();
				map.put("method", "24");
				map.put("instrumentname", nickname);
				map.put("bankname", bankName);
				map.put("accountnumber", bankAccno);
				map.put("accounttype", accType);
				map.put("dlimit", instruDlimit);
				map.put("mlimit", instruMlimit);
				map.put("instid", instrumentObj.getIntruId());
				map.put("currencyId",currency_Id);
				String json = new Converter().mapToJson(map);
				System.out.println("json String :" + json);
				Services serviceproxy = new ServicesProxy();

				String output = serviceproxy.service(json);
				// ---------------------------
				// WebsProxy proxy=new WebsProxy();
				// String output=proxy.updateBankInstru(nickname, bankName,
				// bankAccno, accType, instruDlimit, instruMlimit,
				// instrumentObj.getIntruId());
				System.out.println("Update data from web Service:=" + output);
				HttpSession session = request.getSession();
				if (output.equals("Success")) {
					// --New CODE-------------
					Map<String, String> map1 = new HashMap<String, String>();
					map1.put("method", "16");
					map1.put("mnum", mobilenumber);
					json = new Converter().mapToJson(map1);
					System.out.println("json String :" + json);
					Services serviceproxy1 = new ServicesProxy();

					String out = serviceproxy1.service(json);
					// ---------------------------
					// String out=proxy.fetchInstruments(mobilenumber);
					System.out.println("out=" + out);
					WalletData walletObj = (WalletData) session
							.getAttribute("walletObj");
					walletObj.setInstru(out);
					session.setAttribute("success", "Instrument Updated");
					session.setAttribute("instrumentObj", null);
					// target="jsp/viewprofile.jsp";
					response.sendRedirect("jsp/viewprofile.jsp");

				} else {
					// target="jsp/addcard.jsp";
					session.setAttribute("error", "Instrument Not Added");
					response.sendRedirect("jsp/addinstrument.jsp");

				}
			} else {
				String mobilenumber = (String) request.getSession()
						.getAttribute("username");
				String nickname = request.getParameter("nickname");
				String bankAccno = request.getParameter("bankAccno");
				String bankName = request.getParameter("bankName");
				String accType = request.getParameter("acctype");
				String instruDlimit = request.getParameter("instruDlimit");
				String instruMlimit = request.getParameter("instruMlimit");
				String currency_Id = request.getParameter("currency");
				System.out.println(mobilenumber + " " + nickname + " "
						+ bankAccno + " " + accType);
				// --New CODE-------------
				Map<String, String> map = new HashMap<String, String>();
				map.put("method", "5");
				map.put("mnum", mobilenumber);
				map.put("nickname", nickname);
				map.put("bankname", bankName);
				map.put("accountnumber", bankAccno);
				map.put("accounttype", accType);
				map.put("dlimit", instruDlimit);
				map.put("mlimit", instruMlimit);
				map.put("currencyId", currency_Id);
				String json = new Converter().mapToJson(map);
				System.out.println("json String :" + json);
				Services serviceproxy = new ServicesProxy();

				String output = serviceproxy.service(json);
				// ---------------------------
				// WebsProxy proxy=new WebsProxy();
				// String output=proxy.addInstrumentBank(mobilenumber, nickname,
				// bankAccno, bankName, accType, instruDlimit, instruMlimit);
				System.out.println(" Save data from web Service:=" + output);
				HttpSession session = request.getSession();
				if (output.equals("Success")) {
					// --New CODE-------------
					Map<String, String> map1 = new HashMap<String, String>();
					map1.put("method", "16");
					map1.put("mnum", mobilenumber);
					json = new Converter().mapToJson(map1);
					System.out.println("json String :" + json);
					Services serviceproxy1 = new ServicesProxy();

					String out = serviceproxy1.service(json);
					// ---------------------------
					// String out=proxy.fetchInstruments(mobilenumber);
					System.out.println("out=" + out);

					WalletData walletObj = (WalletData) session
							.getAttribute("walletObj");
					walletObj.setInstru(out);
					// session.setAttribute(walletObj, out);
					// target="jsp/viewprofile.jsp";
					session.setAttribute("success", "Instrument Added");
					response.sendRedirect("jsp/viewprofile.jsp");
				} else {
					// target="jsp/addcard.jsp";
					session.setAttribute("error", "Instrument Not Added");
					response.sendRedirect("jsp/addinstrument.jsp");
				}
			}

		}
		// request.getRequestDispatcher(target).forward(request, response);

	}
}
